package com.example.administrator.fangbook.importlocal.bean;

import android.text.TextUtils;

import com.example.administrator.fangbook.importlocal.utils.FileType;
import com.example.administrator.fangbook.importlocal.utils.HanZi2PinYinUtil;

import net.sourceforge.pinyin4j.PinyinHelper;

import java.io.File;

public class FileNode {
    /**
     * 注意：中文书城图书，fullName 为图书名称， path为图书id
     */

    private FileNode parent;
    private File file; //文件
    private String fullName; //文件
    private String name;
    private FileType type;
    private String path;
    private char firstChar = '#';
    private String author;

    public FileNode(FileNode parent, File file) {
        this.parent = parent;
        this.file = file;
        path = file.getAbsolutePath();
        fullName = file.getName();
        if (file.isDirectory()) {
            type = FileType.DIR;
        } else {
            String fileName = fullName.toLowerCase();
            if (fileName.endsWith(".txt")) {
                type = FileType.TXT;
            } else if (fileName.endsWith(".epub")) {
                type = FileType.EPUB;
            } else if (fileName.endsWith(".zb")) {
                type = FileType.CHINESE_ALL;
            } else {
                type = FileType.UNKNOWN;
            }
            int index = fullName.lastIndexOf(".");
            if (index != -1) {
                name = fullName.substring(0, index);
            }
        }
    }

    /**
     * 拼音首字母排序
     */
    public void createFirstPinYinChar() {
        if (fullName.length() > 0) {
            if (HanZi2PinYinUtil.isHanZiStart(fullName)) {
                //是汉字开始
                String[] py = PinyinHelper.toHanyuPinyinStringArray(fullName.charAt(0));//.toTongyongPinyinStringArray(fullName.charAt(0));
                if (py != null && py.length > 0 && !TextUtils.isEmpty(py[0])) {
                    char tmp = py[0].toUpperCase().charAt(0);
                    if (tmp >= 'A' && tmp <= 'Z') {
                        firstChar = tmp;
                    }
                }
            } else {
                //是英文开始
                char tmp = fullName.toUpperCase().charAt(0);
                if (tmp >= 'A' && tmp <= 'Z') {
                    firstChar = tmp;
                }
            }
        }
    }

    public FileNode getParent() {
        return parent;
    }

    public void setParent(FileNode parent) {
        this.parent = parent;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getName() {
        if (TextUtils.isEmpty(name)) {
            this.name = fullName;
        }
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public FileType getType() {
        return type;
    }

    public void setType(FileType type) {
        this.type = type;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public char getFirstChar() {
        return firstChar;
    }

    public void setFirstChar(char firstChar) {
        this.firstChar = firstChar;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public boolean isDirectory() {
        return type.equals(FileType.DIR);
    }

}
